﻿using System;
using System.Collections.Generic;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;
using CkSoftware.GroupMe.Sdk.Messaging.Commands;
using CkSoftware.GroupMe.Sdk.Messaging.Dao;
using CkSoftware.GroupMe.Sdk.Messaging.Enums;

namespace CkSoftware.GroupMe.Sdk.Messaging
{
	/// <summary>
	/// Manager class for group messages. Handles loading, creating, liking and unliking group messages.
	/// </summary>
	public class GroupMeGroupMessages : GroupMeMessages
	{
		private readonly string _groupId;

		/// <summary>
		/// Instantiates a new <see cref="GroupMeGroupMessages" /> object.
		/// </summary>
		/// <param name="webApiAccess">The <see cref="IWebApiAccess" /> to use for API communication.</param>
		/// <param name="groupId">The group id for which the messages should be handled.</param>
		public GroupMeGroupMessages(IWebApiAccess webApiAccess, string groupId)
			: base(webApiAccess, groupId)
		{
			_groupId = groupId;
		}

		/// <summary>
		/// Instantiates a new CreateMessageCommand for group messages.
		/// </summary>
		/// <param name="sourceGuid">The source-guid to use in the group-message.</param>
		/// <param name="text">The text to use in the group-message.</param>
		/// <param name="attachments">The attachments to use in the group-message.</param>
		/// <returns>The correctly constructed IWebApiCommand.</returns>
		protected override IWebApiCommand<GroupMeApiResponse<GroupMeMessageData>> GetCreateMessageCommand(string sourceGuid,
			string text, IEnumerable<GroupMeAttachmentBase> attachments)
		{
			var command = new GroupMessagesCreateCommand(_groupId, sourceGuid);

			if (!string.IsNullOrEmpty(text))
			{
				command.WithText(text);
			}

			if (attachments != null)
			{
				foreach (GroupMeAttachmentBase attachment in attachments)
				{
					command.WithAttachment(attachment);
				}
			}

			if (!command.IsParametersValid())
			{
				throw new ArgumentException("Please add at least either a text or an attachment.");
			}

			return command;
		}

		/// <summary>
		/// Instantiates a new IndexCommand for group messages.
		/// </summary>
		/// <param name="direction">The direction of the IndexCommand.</param>
		/// <returns>The correctly constructed IWebApiCommand.</returns>
		protected override IWebApiCommand<GroupMeApiResponse<MessagesIndexData>> GetLoadMessagesCommandForDirection(
			MessageLoadDirection direction)
		{
			GroupMessagesIndexParameters parameters = GetParametersForDirection(direction);
			return new GroupMessagesIndexCommand(parameters);
		}

		private GroupMessagesIndexParameters GetParametersForDirection(MessageLoadDirection direction)
		{
			if (!InitialLoadDone)
			{
				return new GroupMessagesIndexParameters(_groupId);
			}

			switch (direction)
			{
				case MessageLoadDirection.Backward:
					return new GroupMessagesIndexParameters(_groupId, OldestMessageId);
				case MessageLoadDirection.Forward:
					return new GroupMessagesIndexParameters(_groupId, sinceId: NewestMessageId);
				default:
					return new GroupMessagesIndexParameters(_groupId);
			}
		}
	}
}